package cn.iocoder.yudao.module.promotion.controller.app.channel;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.promotion.controller.admin.channel.vo.*;
import cn.iocoder.yudao.module.promotion.controller.admin.interviewlog.vo.InterviewLogCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.app.channel.vo.ChannelLinkReqVO;
import cn.iocoder.yudao.module.promotion.convert.channel.ChannelLinkConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.channel.ChannelLinkDO;
import cn.iocoder.yudao.module.promotion.service.channel.ChannelLinkService;
import cn.iocoder.yudao.module.promotion.service.interviewlog.InterviewLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 商城链接配置")
@RestController
@RequestMapping("/promotion/channel-link")
@Validated
public class AppChannelLinkController {

    @Resource
    private ChannelLinkService channelLinkService;

    @Resource
    private InterviewLogService interviewLogService;

    @GetMapping("/get")
    @Operation(summary = "获得商城链接配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ChannelLinkRespVO> getChannelLink(@RequestParam("id") Long id) {
        ChannelLinkDO channelLink = channelLinkService.getChannelLink(id);
        return success(ChannelLinkConvert.INSTANCE.convert(channelLink));
    }

    @GetMapping("/list")
    @Operation(summary = "获得商城链接配置列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<ChannelLinkRespVO>> getChannelLinkList(@RequestParam("ids") Collection<Long> ids) {
        List<ChannelLinkDO> list = channelLinkService.getChannelLinkList(ids);
        return success(ChannelLinkConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商城链接配置分页")
    public CommonResult<PageResult<ChannelLinkRespVO>> getChannelLinkPage(@Valid ChannelLinkPageReqVO pageVO) {
        PageResult<ChannelLinkDO> pageResult = channelLinkService.getChannelLinkPage(pageVO);
        return success(ChannelLinkConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getDetail")
    @Operation(summary = "获得商城链接配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<ChannelLinkRespVO> getDetail(@Valid ChannelLinkReqVO linkVO) throws UnsupportedEncodingException {
        linkVO.setUrl(URLDecoder.decode(linkVO.getUrl(), "UTF-8"));
        ChannelLinkDO channelLink = channelLinkService.getDetail(linkVO);
        if(channelLink != null){
            InterviewLogCreateReqVO interviewLogDO = new InterviewLogCreateReqVO();
            interviewLogDO.setUrl(channelLink.getUrl());
            interviewLogDO.setUrlId(channelLink.getId());
            interviewLogDO.setUserId(getLoginUserId());
            interviewLogDO.setStatus((byte) 0);
            interviewLogService.createInterviewLog(interviewLogDO);
        }
        return success(ChannelLinkConvert.INSTANCE.convert(channelLink));
    }
}
